<template>
  <div class="machineManagement">
    machineManagement
    <div class="search">
      <el-input placeholder="请输入内容" v-model="search" clearable size="large"></el-input>
      <el-button type="primary" icon="el-icon-search">搜索</el-button>
      <el-button type="primary" @click="addMachine">
        新增
        <i class="el-icon-circle-plus el-icon--right"></i>
      </el-button>
    </div>
    <el-table stripe border :data="tableData" style="width: 100%">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="IP" prop="ip"></el-table-column>
      <el-table-column label="机房" prop="groupName"></el-table-column>
      <!-- <el-table-column label="业务数" prop="bizCount"></el-table-column> -->
      <el-table-column label="进程数" prop="processCount"></el-table-column>
      <el-table-column label="剩余内存" prop="leftMemory"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="deleteMachine(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "",

  data() {
    return {
      search: "",
      newMachine: { ip: "" },
      tableData: [
        // {
        //   id: 657789,
        //   // 机器id
        //   ip: "192.168.123.123",
        //   // 机器IP
        //   groupName: "深圳龙岗机房",
        //   // 机房中文名
        //   bizCount: 12,
        //   // 该机器上的业务数
        //   processCount: 23,
        //   // 该机器上的进程数
        //   leftMemory: 8875934
        //   // 剩余内存，单位:KB
        // }
      ]
    };
  },
  methods: {
    getList(){
      this.$ajax({
      method: "get",
      url: "/machine/list"
    })
      .then(res => {
        this.tableData = res.data.data.list;
        this.tableData.forEach(item => {
          item.leftMemory = item.leftMemory / 1024 + "G";
        });
      })
      .catch(err => {
        console.log(err);
      });
    },
    addMachine() {
      this.$prompt("请输入新增机器IP", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          console.log(value);
          this.newMachine.ip = value;
          console.log(this.newMachine);
          this.$ajax({
            method: "POST",
            url: "/machine",
            data: {ip:value}
          })
            .then(res => {
              console.log(res);
              if (res.status == 200) {
                this.$message({
                  type: "success",
                  message: "机器添加成功"
                });
                this.getList();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    },
    deleteMachine(id) {
      this.$confirm("此操作将永久删除该机器, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$ajax({
            method: "delete",
            url: "/machine/" + id
          }).then(res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getList();
            }
          })
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  mounted() {
    this.getList();
  }
};
</script>

<style lang='less' scoped>
.el-input {
  width: 10%;
  margin: 20px 10px 20px 50px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
